package com.guoyw.leetcode.question.answer;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * @Description: 3. 无重复字符的最长子串
 * @Author: guoyw
 * @CreateDate: 2021/3/25 7:18 下午
 * @Version: 1.0
 */
@Slf4j
public  class C3 {

  public static void main(String[] args) {
    int aa = lengthOfLongestSubstring("abcabcbb");
    log.info(aa+"");
  }

  public static int lengthOfLongestSubstring(String s) {
    if(s == null || s.equals("")){
      return 0;
    }

    int max = 0;
    int left = 0;
    HashMap<Character, Integer> map = new HashMap<>();
    for(int i = 0; i < s.length();i++){
      if (map.containsKey(s.charAt(i))) {
        left = Math.max(left,map.get(s.charAt(i))+1);
      }
      map.put(s.charAt(i),i);
      max = Math.max(max, i - left+1);
    }
    return max;
  }
}
